package com.ynwa.thread;

/**
 * 线程中断测试
 *
 * @author gzd
 * @date 2019-09-28 21:41
 */
public class InterruptedTest {

    private static Object object = new Object();

    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = () -> {
            try {
                while (true) {
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                System.out.println("我被中断了(sleep test)");
                System.out.println("中断位: " + Thread.interrupted());
            }

        };

        Runnable runnable1 = () -> {
            while (true) {
            }
        };

        Runnable runnable2 = () -> {
            synchronized (object) {
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };


        Thread sleep = new Thread(runnable, "sleep");
        sleep.start();

        Thread deadWhile = new Thread(runnable1, "dead while");
        deadWhile.start();

//        Thread thread = new Thread(runnable3);
//        thread.start();

        Thread wait = new Thread(runnable2, "wait");
        wait.start();


        Thread.sleep(10000);

        sleep.interrupt();
        deadWhile.interrupt();
        wait.interrupt();

//        new Thread(runnable2, "wait").start();
    }
}
